package com.ym.jiujiangeyou;

import java.util.ArrayList;
import java.util.List;

import cn.sharesdk.sina.weibo.SinaWeibo;
import cn.sharesdk.tencent.weibo.TencentWeibo;

import com.ym.jiujiangeyou.app.AppManager;
import com.ym.jiujiangeyou.bitmap.SimpleImageLoad;
import com.ym.jiujiangeyou.entity.Food;
import com.ym.jiujiangeyou.ext.SatelliteMenu;
import com.ym.jiujiangeyou.ext.SatelliteMenuItem;
import com.ym.jiujiangeyou.ext.SatelliteMenu.SateliteClickedListener;
import com.ym.jiujiangeyou.util.FileManager;
import com.ym.jiujiangeyou.weibo.sdk.BaseType;
import com.ym.jiujiangeyou.weibo.sdk.ShareActivity;
import com.ym.jiujiangeyou.weibo.sdk.ShareItem;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

/**
 * @author 叶铭
 * @email yeming_1001@163.com
 * @version V1.0
 */
public class FoodDetails extends Activity implements SateliteClickedListener{
	
	private final static int QZONE = 1;
	private final static int SINA_ID = 2;
	private final static int WECHAT_ID = 3;
	private final static int WECHAT_MOM_ID = 4;
	private final static int TENCENT_ID = 5;
	private TextView title, food_feature, food_introduction;
	private ImageView food_img;
	private Food food = null;
	private SimpleImageLoad simpleImageLoad;
	private LinearLayout title_back;

	private BaseType.ListItem mTypeItem = new BaseType.ListItem();
	private BaseType.InfoItemEx mInfoItem = new BaseType.InfoItemEx();
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		// 添加Activity到堆栈
		AppManager.getAppManager().addActivity(this);
		
		super.setContentView(R.layout.food_details);

		food = getData();
		init();

	}

	public Food getData() {
		Bundle bundle = getIntent().getBundleExtra("food");
		food = new Food(bundle.getString("name"),
				bundle.getString("introduct"), bundle.getString("url"),
				bundle.getString("feature"));
		return food;
	}

	public void init() {
		title = (TextView) this.findViewById(R.id.title_text);
		food_feature = (TextView) this.findViewById(R.id.food_feature);
		food_introduction = (TextView) this
				.findViewById(R.id.food_introduction);
		food_img = (ImageView) this.findViewById(R.id.food_img);
		title_back = (LinearLayout) this.findViewById(R.id.title_back);
		simpleImageLoad = new SimpleImageLoad(this);
		SatelliteMenu satellite_menu = (SatelliteMenu) findViewById(R.id.satellite_menu);

		List<SatelliteMenuItem> items = new ArrayList<SatelliteMenuItem>();
		items.add(new SatelliteMenuItem(QZONE, R.drawable.logo_qzone));
		items.add(new SatelliteMenuItem(WECHAT_ID, R.drawable.logo_wechat));
		items.add(new SatelliteMenuItem(WECHAT_MOM_ID,
				R.drawable.logo_wechatmoments));
		items.add(new SatelliteMenuItem(SINA_ID, R.drawable.logo_sina));
		items.add(new SatelliteMenuItem(TENCENT_ID,
				R.drawable.logo_tencentweibo));
		satellite_menu.addItems(items);
		satellite_menu.setOnItemClickedListener(this);
		
		simpleImageLoad.DisplayImage(food.getUrl(), food_img);
		title.setText(food.getName());
		food_feature.setText(food.getFeature());
		food_introduction.setText(food.getIntroduct());

		title_back.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				FoodDetails.this.onBackPressed();
			}
		});
		
		food_img.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				if (food.getUrl() != null
						&& FileManager.getBitmap(food.getUrl()) != null) {
					Intent intent = new Intent(FoodDetails.this,
							PicDetailActivity.class);
					intent.putExtra("imageurl", food.getUrl());
					startActivity(intent);
			}
			}
		}) ;
	}
	/**
	 * 
	 * @retur void
	 * @Description: 分享到sina
	 */
	private void shareToSina() {
		// LAroundApplication.getInstance().onEvent("UMID0008");
		ShareItem.clear();

		String imageURL = mInfoItem.getImageURL(0);
		if (food != null) {
			ShareItem.setText(food.getName() + "     ---"
					+ food.getFeature());
		} else {
			ShareItem.setText(mInfoItem.mContent);
		}
		// if (imageURL != null){
		// ShareItem.setImageUrl(imageURL);
		// ShareItem.setImageUrl(scenery.getImageurl());
		// String sharPath = fileCache.getSavePath(imageURL);
		ShareItem.setShareImagePath(FileManager.getImagePath(food
				.getUrl()));
		// }
		ShareItem.setPlatform(SinaWeibo.NAME);
		goShareActivity();
	}

	/**
	 * 
	 * @retur void
	 * @Description: 分享到tencent微博
	 */
	private void shareToTencent() {
		ShareItem.clear();

		// String imageURL = mInfoItem.getImageURL(0);
		if (food != null) {
			ShareItem.setTitle(food.getName());
			ShareItem.setText(food.getFeature());
		} else {
			ShareItem.setTitle(mInfoItem.mTitle);
			// ShareItem.setTitleUrl("http://blog.csdn.net/lancees");
			ShareItem.setText(mInfoItem.mContent);
		}
		// if (imageURL != null){
		// String sharPath = fileCache.getSavePath(imageURL);
		ShareItem.setImagePath(FileManager.getImagePath(food.getUrl()));
		ShareItem.setShareImagePath(FileManager.getImagePath(food.getUrl()));
		// }
		ShareItem.setPlatform(TencentWeibo.NAME);
		goShareActivity();
	}

	/**
	 * 
	 * @retur void
	 * @Description: go to分享activity
	 */
	private void goShareActivity() {

		Intent intent = new Intent();
		intent.setClass(this, ShareActivity.class);
		startActivity(intent);
	}
	@Override
	public void eventOccured(int id) {
		// TODO Auto-generated method stub
		switch (id) {
		case SINA_ID:
			shareToSina();
			break;
		case TENCENT_ID:
			shareToTencent();
			break;
		case WECHAT_ID:
			Toast.makeText(this, "开发中ing", Toast.LENGTH_SHORT).show();
			break;
		case WECHAT_MOM_ID:
			Toast.makeText(this, "开发中ing", Toast.LENGTH_SHORT).show();
			break;
		case QZONE:
			Toast.makeText(this, "开发中ing", Toast.LENGTH_SHORT).show();
			break;
		}
	}

}
